<template>
  <div class="login_container">
    <div class="login_box">
      <img class="logo" src="../assets/logo.png" alt />
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item prop="username">
          <el-input
            placeholder="请输入内容"
            prefix-icon="iconfont icon-user"
            v-model="ruleForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入内容"
            type="password"
            prefix-icon="iconfont icon-3702mima"
            v-model="ruleForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
           
<script>
//引入api文件里的登录请求方法
import { login_api } from "../api/index";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      //表单验证
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "长度在 6 到 12个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //登录二次校验
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        //校验成功
        if (valid) {
          //发送请求
          const res = await login_api(this.ruleForm);
          window.sessionStorage.setItem('user',JSON.stringify(res.data.data))
          //判断请求是否成功
          if (res.data.meta.status === 200) {
            this.$message({
              showClose: true,
              message: res.data.meta.msg,
              type: "success",
            });
            //保存token
            window.sessionStorage.setItem('token', res.data.data.token)
            // 导航至/home
            this.$router.push('/welcome')
          } else {
            this.$message({
              showClose: true,
              message: res.data.meta.msg,
              type: "error",
            });
          }
        } else {
          return false;
        }
      });
    },
    //重置按钮点击事件
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped lang='less'>
.login_container {
  background-color: purple;
  height: 100%;
  position: relative;
  .login_box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #fff;
    img.logo {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      border: 10px solid #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #ccc;
      box-shadow: 0 0 10px #ddd;
    }
    .demo-ruleForm {
      margin-top: 120px;
      .el-form-item {
        width: 90%;
        margin: 0px auto 20px;
        .el-button--primary {
          margin-left: 253px;
        }
      }
    }
  }
}
</style>
